package br.org.isvi.interpretador.portugol.types;

public class Variable extends SymbolImpl{
	private String name;
	private VariableType type;
	
	public Variable(VariableType type, Object value, String name) {
		super(Kind.VARIAVEL, value);
		this.name = name;
		this.type = type;
	}

	public Variable(VariableType type, String name) {
		super(Kind.VARIAVEL, "");
		this.name = name;
		this.type = type;
		
		if(type == VariableType.inteiro) {
			setValue(IntegerSymbol.valueOf("0"));
		} else if(type == VariableType.real) {
				setValue(RealSymbol.valueOf("0"));
		} else if(type == VariableType.literal) {
				setValue(new StringSymbol(""));
		} else if(type == VariableType.booleano) {
			setValue(BooleanSymbol.FALSO);
		}
	}
	
	public String getName() {
		return name;
	}
	
	public VariableType getType() {
		return type;
	}
	
	public static String getVariableValueAsString(Variable v) {
		String ret = null;
		
		switch (v.getType()) {
		case inteiro:
			ret = ((IntegerSymbol)v.getValue()).getValue().toString();
			break;
		case real:
			ret = ((RealSymbol)v.getValue()).getValue().toString();
			break;
		case literal:
			ret = ((StringSymbol)v.getValue()).getValue().toString();
			break;	
		case booleano:
			ret = ((Boolean)((BooleanSymbol)v.getValue()).getValue())? "VERDADEIRO" : "FALSO";
			break;		
		default:
			break;
		}
		
		return ret;
	}
	
	@Override
	public String toString() {
		return getValue().toString();
	}

	public boolean bigger(Symbol symb) {
		return ((Symbol)this.getValue()).bigger(symb);
	}

	public boolean equals(Symbol symb) {
		return ((Symbol)this.getValue()).equals(symb);
	}
	
	@Override
	public Kind getKind() {
		return Kind.VARIAVEL;
	}
	
	
	public static Symbol castToVarTypeIfPossible(Symbol value, Variable var) {
		Symbol ret = value;
		
		if(value instanceof NumberSymbol) {
			if(var.getType() == VariableType.inteiro) {
				
				ret = new IntegerSymbol(((Number)value.getValue()).intValue());
				
			} else if(var.getType() == VariableType.real) {
				
				ret = new RealSymbol(((Number)value.getValue()).doubleValue());
				
			}
		}
		
		return ret;
	}

	public boolean smaller(Symbol symb) {
		return ((Symbol)this.getValue()).smaller(symb);
	}	
}
